package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxbbc.common.core.entity.jz.AgAgent;
import com.sxbbc.common.core.entity.jz.AgFreezeMoneyDetail;
import com.sxbbc.common.core.entity.jz.AgMoneyDetail;
import com.sxbbc.common.core.enums.FreezeStatusEnums;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import com.sxbbc.common.core.mapper.jz.AgAgentMapper;
import com.sxbbc.common.core.mapper.jz.AgMoneyDetailMapper;
import com.sxbbc.common.core.service.IAgFreezeMoneyDetailService;
import com.sxbbc.common.core.service.IAgMoneyDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 代理共享值明细 服务实现类
 * </p>
 *
 * @author yuuG
 * @since 2019-08-21
 */
@Service
public class AgMoneyDetailServiceImpl extends ServiceImpl<AgMoneyDetailMapper, AgMoneyDetail> implements IAgMoneyDetailService {

    @Autowired
    private AgMoneyDetailMapper agMoneyDetailMapper;
    @Autowired
    private AgAgentMapper agAgentMapper;
    @Autowired
    private IAgFreezeMoneyDetailService iAgFreezeMoneyDetailService;
    @Override
    public BigDecimal querySumCash(Integer agid) {
        return agMoneyDetailMapper.querySumCash(agid);
    }

    @Override
    public IPage<AgMoneyDetail> handleSelectMoneyDetails(BasePage<AgMoneyDetail> page, Integer cid) {

        QueryWrapper<AgAgent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",cid);
        AgAgent agent = agAgentMapper.selectOne(queryWrapper);

        IPage<AgMoneyDetail> agMoneyDetailIPage = agMoneyDetailMapper.handleSelectMoneyDetails(page, agent.getId());
        if(agMoneyDetailIPage.getRecords().size()>0){
            for (AgMoneyDetail detail : agMoneyDetailIPage.getRecords()) {
                detail.setTypeName(HandleTypeEnums.getName(detail.getTypeId()));
            }
        }
        return agMoneyDetailIPage;
    }

    @Override
    public IPage<AgFreezeMoneyDetail> handleSelectFreezeMoneyDetails(BasePage<AgFreezeMoneyDetail> page, Integer cid) {

        QueryWrapper<AgAgent> agentQueryWrapper = new QueryWrapper<>();
        agentQueryWrapper.eq("cid",cid);
        AgAgent agent = agAgentMapper.selectOne(agentQueryWrapper);


        QueryWrapper<AgFreezeMoneyDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("agid", agent.getId()).orderByDesc("create_time");
        IPage<AgFreezeMoneyDetail> moneyDetailIPage = iAgFreezeMoneyDetailService.page(page, queryWrapper);
        if (moneyDetailIPage.getRecords().size() > 0) {
            for (AgFreezeMoneyDetail record : moneyDetailIPage.getRecords()) {
                record.setStatusName(FreezeStatusEnums.getName(record.getId()));
                record.setTypeName(HandleTypeEnums.getName(record.getTypeId()));
            }
        }
        return moneyDetailIPage;
    }
}
